<template>
  <div class="app-container">

    <div class="tabInfo">
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      class="demo-form-inline"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable

        />
      </el-form-item>

      <el-form-item label="开始时间" prop="deployTime">
        <el-date-picker
          clearable

          v-model="queryParams.deployTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"

          @click="handleQuery"
          >
          <el-icon><Search /></el-icon>
          搜索</el-button

        >
        <el-button   @click="resetQuery"
          >
          <el-icon><Refresh /></el-icon>
          重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:deployment:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="finishedList1"
            @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="任务编号"
        align="center"
        prop="taskId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="流程名称"
        align="center"
        prop="procDefName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="任务节点" align="center" prop="taskName" />
      <el-table-column label="流程发起人" align="center">
        <template #default="scope">
          <label
            >{{ scope.row.startUserName }}
            <el-tag type="info" size="mini">{{
              scope.row.startDeptName
            }}</el-tag></label
          >
        </template>
      </el-table-column>
      <el-table-column
        label="接收时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column
        label="审批时间"
        align="center"
        prop="finishTime"
        width="180"
      />
      <el-table-column
        label="耗时"
        align="center"
        prop="duration"
        width="180"
      />
      <el-table-column
        label="操作"
        width="150"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleFlowRecord(scope.row)"
            >流转记录</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-left"
            @click="handleRevoke(scope.row)"
            >撤回</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  finishedList,
  getDeployment,
  delDeployment,
  addDeployment,
  updateDeployment,
  exportDeployment,
} from "@/api/flowable/finished";

// revokeProcess
// import {
//   finishedList,
//   delDeployment,
//   revokeProcess,
// } from "@/api/flowable/finished";

const route = useRoute();
const router = useRouter();
const loading = ref(true);
const ids = ref([]);
const multiple = ref(true);
const single = ref(true);
const showSearch = ref(true);
const total = ref(0);
const finishedList1 = ref([]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: null,
  category: null,
  key: null,
  tenantId: null,
  deployTime: null,
  derivedFrom: null,
  derivedFromRoot: null,
  parentDeploymentId: null,
  engineVersion: null,
});

const getList = () => {
  loading.value = true;
  finishedList1.value = []; // 初始化列表数据

  console.log(queryParams)
  finishedList(queryParams).then((response) => {
    finishedList1.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
};

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryParams.name = null;
  queryParams.deployTime = null;
  handleQuery();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const handleFlowRecord = (row) => {
  console.log(row, "row");
  router.push({
    path: "/flowable/task/finished/detail/index",
    query: {
      procInsId: row.procInsId,
      deployId: row.deployId,
      taskId: row.taskId,
      taskDefKey: row.taskDefKey,
    },
  });
};

// const handleRevoke = (row) => {
//   revokeProcess({ instanceId: row.procInsId }).then((res) => {
//     alert(res.msg); // 使用 alert 替代消息提示
//     getList();
//   });
// };

const handleDelete = (row) => {
  const idsToDelete = row.id || ids.value;
  if (confirm(`是否确认删除流程定义编号为"${idsToDelete}"的数据项?`)) {
    delDeployment(idsToDelete).then(() => {
      getList();
      alert("删除成功");
    });
  }
};

onMounted(() => {
  getList();
});
</script>
<style>

.demo-form-inline .el-input {
  --el-input-width: 220px !important;
}

.demo-form-inline .el-select {
  --el-select-width: 220px !important;
}

</style>
